import 'package:flutter/material.dart';
import '../services/settings_service.dart';
import '../theme/app_theme.dart';

class NotificationSettingsPage extends StatefulWidget {
  const NotificationSettingsPage({Key? key}) : super(key: key);

  @override
  _NotificationSettingsPageState createState() => _NotificationSettingsPageState();
}

class _NotificationSettingsPageState extends State<NotificationSettingsPage> {
  final SettingsService _settingsService = SettingsService();
  bool _isLoading = true;
  bool _enableAllNotifications = true;
  bool _taskReminders = true;
  String _reminderTime = '15 分钟前';

  @override
  void initState() {
    super.initState();
    _loadSettings();
  }

  Future<void> _loadSettings() async {
    final settings = await _settingsService.loadNotificationSettings();
    setState(() {
      _enableAllNotifications = settings['enableAll'];
      _taskReminders = settings['taskReminders'];
      _reminderTime = settings['reminderTime'];
      _isLoading = false;
    });
  }

  Future<void> _updateSettings() async {
    await _settingsService.saveNotificationSettings(
      enableAll: _enableAllNotifications,
      taskReminders: _taskReminders,
      reminderTime: _reminderTime,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('通知设置'),
        backgroundColor: Colors.transparent,
        elevation: 0,
        foregroundColor: AppTheme.textPrimaryColor,
      ),
      body: _isLoading
          ? const Center(child: CircularProgressIndicator())
          : Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [Color(0xFFF8FAFC), Color(0xFFE2E8F0)],
          ),
        ),
        child: ListView(
          padding: const EdgeInsets.all(AppTheme.paddingL),
          children: [
            _buildSection(
              title: '主要设置',
              children: [
                _buildSwitchItem(
                  title: '开启所有通知',
                  value: _enableAllNotifications,
                  onChanged: (val) {
                    setState(() {
                      _enableAllNotifications = val;
                      if (!val) {
                        _taskReminders = false;
                      }
                    });
                    _updateSettings();
                  },
                ),
              ],
            ),
            const SizedBox(height: AppTheme.paddingL),
            _buildSection(
              title: '任务提醒',
              children: [
                _buildSwitchItem(
                  title: '任务提醒',
                  subtitle: '在任务开始前收到提醒',
                  value: _taskReminders,
                  onChanged: _enableAllNotifications
                      ? (val) {
                          setState(() => _taskReminders = val);
                          _updateSettings();
                        }
                      : null,
                ),
                _buildDropdownItem(
                  title: '提醒时间',
                  value: _reminderTime,
                  items: ['5 分钟前', '15 分钟前', '30 分钟前', '1 小时前'],
                  onChanged: _enableAllNotifications && _taskReminders
                      ? (val) {
                          setState(() => _reminderTime = val!);
                          _updateSettings();
                        }
                      : null,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSection({required String title, required List<Widget> children}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(left: AppTheme.paddingS, bottom: AppTheme.paddingS),
          child: Text(
            title,
            style: const TextStyle(
              fontSize: AppTheme.fontSizeL,
              fontWeight: FontWeight.bold,
              color: AppTheme.textPrimaryColor,
            ),
          ),
        ),
        Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(AppTheme.radiusL),
            boxShadow: AppTheme.softShadow,
          ),
          child: Column(children: children),
        ),
      ],
    );
  }

  Widget _buildSwitchItem({
    required String title,
    String? subtitle,
    required bool value,
    required ValueChanged<bool>? onChanged,
  }) {
    return ListTile(
      title: Text(title, style: TextStyle(color: onChanged == null ? Colors.grey : AppTheme.textPrimaryColor)),
      subtitle: subtitle != null ? Text(subtitle, style: const TextStyle(fontSize: AppTheme.fontSizeS)) : null,
      trailing: Switch(
        value: value,
        onChanged: onChanged,
        activeColor: AppTheme.primaryColor,
      ),
      onTap: () {
        if (onChanged != null) {
          onChanged(!value);
        }
      },
    );
  }

  Widget _buildDropdownItem({
    required String title,
    required String value,
    required List<String> items,
    required ValueChanged<String?>? onChanged,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(title, style: TextStyle(color: onChanged == null ? Colors.grey : AppTheme.textPrimaryColor, fontSize: 16)),
          DropdownButton<String>(
            value: value,
            items: items.map((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );
            }).toList(),
            onChanged: onChanged,
            underline: Container(),
            style: TextStyle(color: onChanged == null ? Colors.grey : AppTheme.textPrimaryColor, fontSize: 16),
          ),
        ],
      ),
    );
  }
} 